/* eslint-disable max-len */
/* eslint-disable @iceworks/best-practices/no-secret-info */


import { default as messages } from '@/locales';
import Notification from '@alifd/next/lib/notification';
import { i18n } from '@lingui/core';
import RSocket from '@tau/rsocket';
import { request as Ajax, util as Util, getLocale, persist } from '@tau/utils';
import dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';
import duration from 'dayjs/plugin/duration';
import relativeTime from 'dayjs/plugin/relativeTime';
import _ from 'lodash';
import { configure as mobxConfig, runInAction, toJS } from 'mobx';
import Cookies from 'universal-cookie';
import config from './config';
import * as consts from './consts';
import {
  _SIZ_INDEX_VALUE,
  _SIZE_FORM_VALUE,
  _SIZE_DIALOG_VALUE,
  _SIZE_GLOBAL_VALUE,
} from './_styleSizeSet';

const { Api } = Ajax;

const SCOPE = 'cbv-bob';
const DOMAIN = 'mzd';

// IP+端口时为开发调试，没有ssl
// const WS_PROTO = location.port ? 'ws' : 'wss';
const PROTO = location.protocol === 'https:' ? 'wss' : 'ws';

// 国际化配置
// const LOCALE = 'en-US';
const LOCALE = getLocale(SCOPE);

// _.merge(messages, tauMessages);

i18n.load(messages);
i18n.activate(LOCALE);

dayjs.extend(duration);
dayjs.extend(relativeTime);

if (dayjs) {
  // const momentDurationFormatSetup = require('moment-duration-format');
  // momentDurationFormatSetup(moment);
  const locale = LOCALE.toLowerCase();
  // require(`dayjs/locale/${locale}`);
  dayjs.locale(locale);
}

RSocket.setDebug(true);

const WS_URL = `${PROTO}://${location.host}/rsocket`;

const API_BASE = '/v2';
let ENV;

Notification.config({ placement: 'topRight' });

mobxConfig({
  enforceActions: 'never',
});

const EVENT = {
  TOKEN_READY: 'TOKEN_READY', // 认证或token加载成功
  TOKEN_FAILED: 'TOKEN_FAILED', // 认证失败
  USER_READY: 'USER_READY', // 当前用户信息就绪
  LOGOUT: 'LOGOUT', // 注销时触发，监听该事件清理Store中的缓存数据
};
const Events = {
  emit(name, elem) {
    const evt = new Event(name);
    if (elem) {
      elem.dispatchEvent(evt);
    } else {
      window.dispatchEvent(evt);
    }
  },

  on(name, handler) {
    window.addEventListener(name, handler);
  },

  off(name, handler) {
    window.removeEventListener(name, handler);
  },
};

// 配置全局ENV
function getEnvCfg() {
  const envCfg = config[global.ENV];
  return envCfg;
}

const currencySign = (c) => {
  switch (c) {
    case 'HKD':
    case 'USD':
    case 'MOP':
      return '$';
    default:
      return '¥';
  }
};

// 跳转小程序版本
const ENV_VERSITION = {
  sit: 'trial',
  uat: 'release',
};

Object.assign(global, {
  ...consts,
  i18n,
  // t: (msg) => i18n._(msg), // 供@tau/ui使用
  LOCALE,
  scopePrefixes: ['cbv'],
  WS_URL,
  SCOPE,
  // 全局本地化函数
  FAIL(content) { Notification.error({ title: t`错误`, content, size: 'medium', duration: 0 }); },
  SUCC(content) { Notification.success({ title: t`成功`, content, size: 'medium' }); },
  API_BASE,
  DOMAIN,
  TOKEN: '',
  TENANT: 'mzd',
  Api,
  Ajax,
  Util,
  RSocket,
  ROUTE: Util.escapeRoute,
  toJS,
  runInAction,
  Events,
  EVENT,
  persist,
  config,
  ENV,
  getEnvCfg,
  _,
  SIZE: localStorage.getItem('pageSize') ? localStorage.getItem('pageSize') * 1 : 20,
  Cookie: new Cookies(null, { path: '/' }),
  DATE_FORMAT: 'YYYY-MM-DD',
  DATETIME_FORMAT: 'YYYY-MM-DD HH:mm',
  DATETIMES_FORMAT: 'YYYY-MM-DD HH:mm:ss',
  TIME_FORMAT: 'HH:mm',
  currencySign,
  toCNY: (v) => i18n.number(v, { style: 'currency', currency: 'CNY' }),
  _SIZ_INDEX_VALUE,
  _SIZE_FORM_VALUE,
  _SIZE_DIALOG_VALUE,
  _SIZE_GLOBAL_VALUE,
  ENV_VERSITION,
});

let rootStore;
(function () {
  if (!rootStore) globalThis.rootStore = require('./stores').default;
}());
